﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;

namespace AdjustTheGradient
{
    class AdjustTheGradient:Window
    {
        LinearGradientBrush brush;

        [STAThread]
        public static void Main()
        {
            Application app = new Application();
            app.Run(new AdjustTheGradient());
;        }

        public AdjustTheGradient()
        {
            this.Title = "Adjust the Gradient";
            this.SizeChanged += this.AdjustTheGradient_SizeChanged;
            this.brush = new LinearGradientBrush(Colors.Red, Colors.Blue, 0);
            this.brush.MappingMode = BrushMappingMode.Absolute;
            this.Background = brush;
        }

        private void AdjustTheGradient_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            double width = this.ActualWidth - 2 * SystemParameters.ResizeFrameVerticalBorderWidth;
            double height = this.ActualHeight - 2 * SystemParameters.ResizeFrameHorizontalBorderHeight - SystemParameters.CaptionHeight;

            Point ptCenter = new Point(width / 2, height / 2);
            Vector vectDiag = new Vector(width, -height);
            Vector vectPerp = new Vector(vectDiag.Y, -vectDiag.X);

            vectPerp.Normalize();
            vectPerp *= width * height / vectDiag.Length;

            brush.StartPoint = ptCenter + vectPerp;
            brush.EndPoint = ptCenter - vectPerp;
        }
    }
}
